package com.link.base.tencent.corpwx.externalcontact.service;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.base.tencent.corpwx.agent.util.AgentIdUtil;
import com.link.base.tencent.corpwx.externalcontact.util.ExternalContactUtil;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;
import com.link.base.tencent.corpwx.externalcontact.dao.mybatis.mapper.GroupChatDataMapper;
import com.link.base.tencent.corpwx.externalcontact.model.GroupChatData;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.Date;

/**
 * 客户群统计数据 
 *
 * @author 欧阳振强
 * @date 2020-03-13 21:14:07.0
 */
@Service
public class GroupChatDataServiceImpl extends BasicServiceImpl<GroupChatData> implements GroupChatDataService {

    @Resource
    private GroupChatDataMapper groupChatDataMapper;
    @Resource
    private AgentService agentService;
    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<GroupChatData> getBasicMapper() {
        return groupChatDataMapper;
    }

    @Override
    public void getGroupChatData() throws Exception {
        // 设置数据起始时间 - 时间戳和时间对象
        String startTime;
        String startDate;
        GroupChatData param = new GroupChatData();
        GroupChatData groupChatData = groupChatDataMapper.getLastRecordByStatTime(param);
        if (groupChatData == null) {
            // 不存在现有数据，则默认查询昨天的
            Date date = DateUtil.addDayToDate(new Date(), -1);
            startDate = DateUtil.dateToStr(date, "yyyy-MM-dd");
            startTime = String.valueOf(date.getTime() / 1000);
        } else {
            Date existStartDate = DateUtil.addDayToDate(DateUtil.stringToDate(groupChatData.getStartDate(), "yyyy-MM-dd"), 1);
            startDate = DateUtil.dateToStr(existStartDate, "yyyy-MM-dd");
            startTime = String.valueOf(existStartDate.getTime() / 1000);
        }
        String accessToken = agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID);
        // 数据总量
        String total = "-1";
        // 当前偏移量
        String nextOffset = "0";
        // 每页数量
        String limit = "500";
        // 当next_offset和total相等时，说明已经取完所有
        while (!total.equals(nextOffset)) {
            JSONObject object = ExternalContactUtil.getGroupChatData(accessToken, null, null, startTime, null, null, nextOffset, limit);
            total = object.getString("total");
            nextOffset = object.getString("next_offset");
            JSONArray array = object.getJSONArray("items");
            GroupChatData dayResult = new GroupChatData();
            dayResult.setId(keyGenerateService.keyGenerate());
            dayResult.setStartDate(startDate);
            JSONObject jsonData;
            // 截至当天客户群总数量
            int chatTotal = 0;
            // 当天新增客户群数量
            int newChatCnt = 0;
            // 当天有发过消息的客户群数量
            int chatHasMsg = 0;
            // 当天客户群新增群人数
            int newMemberCnt = 0;
            // 截至当天客户群总人数
            int memberTotal = 0;
            // 当天有发过消息的群成员数
            int memberHasMsg = 0;
            // 当天客户群消息总数
            int msgTotal = 0;
            for (int i = 0; i < array.size(); i++) {
                /**
                 * TODO: 2020/3/14 按照当前逻辑，每日客户群统计数据先不考虑按群主区分，直接就每一个群主的数据合并为当日数据
                 */
                jsonData = array.getJSONObject(i).getJSONObject("data");
                chatTotal += jsonData.getIntValue("chat_total");
                memberTotal += jsonData.getIntValue("member_total");
                newChatCnt += jsonData.getIntValue("new_chat_cnt");
                chatHasMsg += jsonData.getIntValue("chat_has_msg");
                newMemberCnt += jsonData.getIntValue("new_member_cnt");
                memberHasMsg += jsonData.getIntValue("member_has_msg");
                msgTotal += jsonData.getIntValue("msg_total");
            }
            dayResult.setChatTotal(String.valueOf(chatTotal));
            dayResult.setNewChatCnt(String.valueOf(newChatCnt));
            dayResult.setChatHasMsg(String.valueOf(chatHasMsg));
            dayResult.setNewMemberCnt(String.valueOf(newMemberCnt));
            dayResult.setMemberTotal(String.valueOf(memberTotal));
            dayResult.setMemberHasMsg(String.valueOf(memberHasMsg));
            dayResult.setMsgTotal(String.valueOf(msgTotal));
            groupChatDataMapper.insert(dayResult);
        }
    }
}
